package tesys.business;

import java.util.List;

import tesys.application.Main;
import tesys.application.Services;
import tesys.objects.User;
import tesys.persistence.DataAccessStub;

public class AccessUsers
{
	private DataAccessStub dataAccess;
	private List<User> users;
	private User user;
	private int currentUser;

	public AccessUsers()
	{
		dataAccess = (DataAccessStub) Services.getDataAccess(Main.dbName);
		users = null;
		user = null;
		currentUser = 0;
	}

	public User getSequential()
	{
		if (users == null)
		{
			users = dataAccess.getUserSequential();
			currentUser = 0;
		}
		if (currentUser < users.size())
		{
			user = (User) users.get(currentUser);
			currentUser++;
		}
		else
		{
			users = null;
			user = null;
			currentUser = 0;
		}
		return user;
	}

	public User getRandom(String userID)
	{
		user = null;
		if (userID.trim().equals(""))
		{
			//System.out.println("*** Invalid user id");
		}
		else
		{
			users = dataAccess.getUserRandom(new User(userID));
			if (users.size()==1)
			{
				user = (User) users.get(0);
			}
		}
		return user;
	}

	public String insertUser(User currentUser)
	{
		return dataAccess.insertUser(currentUser);
	}

	public String updateUser(User currentUser)
	{
		return dataAccess.updateUser(currentUser);
	}

	public String deleteUser(User currentUser)
	{
		return dataAccess.deleteUser(currentUser);
	}
}
